package cn.tedu.kcircle.attachment.infra.config;


import cn.tedu.kcircle.attachment.infra.minio.MinioTemplate;
import io.minio.MinioClient;
import io.minio.messages.Bucket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.List;

@Configuration
@Slf4j
public class MinioConfig {
    /**
     * 用户名
     */
    @Value("${minio.access-key}")
    private String accessKey;
    /**
     * 密码
     */
    @Value("${minio.secret-key}")
    private String secretKey;
    /**
     * Minio 服务端 api地址
     */
    @Value("${minio.endpoint}")
    private String endpoint;

    /**
     * Minio 默认bucketName
     */
    @Value("${minio.bucket-name}")
    private String defaultBucketName;

    /**
     * 构建 操作Minio的客户端
     * @return
     */
    @Bean
    public MinioClient minioClient() {
        System.out.println(endpoint + "," + accessKey + "," + secretKey);
        MinioClient minioClient = MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
        try{
            List<Bucket> list = minioClient.listBuckets();
            log.info("Bucket List:"+list.size());
        }
        catch(Exception e){
            log.error("minio异常",e);
        }
        return minioClient;
    }
    /**
     * 构建Minio操作集
     */
    @Bean
    public MinioTemplate minioTemplate(MinioClient minioClient){
        return new MinioTemplate(endpoint,defaultBucketName,minioClient);
    }
}
